package caro.app.protocolprovider;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Class ProtocolProcessing
 * @author Thanh Nhan
 * @Since Aug 14, 2011
 * @Version 1.0
 */
public class ProtocolProcessing {
    /**
     * Khoang trang giua cac phan tu trong message
     */
    private static final String BLANK = " ";
    /**
     * Khoang cach giua cac thanh phan lon trong message
     */
    private static final String STAR = "-";
    /**
     * Yeu cau client phai login
     */
    public static final String REQUIRE_LOGIN = "REQUIRE_LOGIN";
    /**
     * Server thong bao cho client biet la dang nhap that bai
     */
    public static final String LOGIN_FAIL = "FAIL";
    /**
     * Server goi mot nuoc co ma client da check den client con lai
     * hoac client goi mot nuoc co ma minh check den server
     */
    public static final String CHECK = "CHECK";
    /**
     * Client goi yeu cau duoc choi voi username
     * hoac server chuyen yeu cau cua username den client con lai
     */
    public static final String PLAY_REQUEST = "PLAY";
    /**
     * Server thong bao Login success
     */
    public static final String LOGIN_SUCCESS = "SUCCESS";
    /**
     * Server thong bao quang ba nguoi dung username moi dang nhap vao
     */
    public static final String NEW = "NEW";
    /**
     * Server goi thong diep bat dau cuoc choi, goi cho ca 2 client
     */
    public static final String START = "START";
    /**
     * Server goi thong bao thang
     */
    public static final String WIN = "WIN";
    /**
     * Server goi thong bao thua
     */
    public static final String LOST = "LOST";
    /**
     * Server goi thong diep rang 2 client busy
     */
    public static final String BUSY = "BUSY";
    /**
     * Server goi thong diep rang 2 client da ranh
     */
    public static final String FREE = "FREE";
    /**
     * Server goi thong diep rang client 1 da roi khoi.
     */
    public static final String LEAVE = "LEAVE";
    /**
     * Client goi thong tin chung thuc
     */
    public static final String AUTH = "AUTH";
    /**
     * client goi thong bao chap nhan yeu cau choi
     */
    public static final String ACCEPT = "ACCEPT";
    /**
     * client goi thong bao tu choi yeu cau choi
     */
    public static final String REFUSE = "REFUSE";
    /**
     * server goi thong bao client da thoat
     */
    public static final String EXIT = "EXIT";
    
    private static final String BR = "\n";
    
    public static String getRequiredLogin(){
        return REQUIRE_LOGIN;
    }
    public static String getLoginFail(){
        return LOGIN_FAIL;
    }
    
    public static String getCheckCell(String username, int r, int c, int value){
        return CHECK.concat(BLANK).concat(username).concat(BLANK)
                .concat(String.valueOf(r)).concat(BLANK).concat(String.valueOf(c))
                .concat(BLANK).concat(String.valueOf(value));
    }
    public static CheckCellPro explainCheckCell(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 5){
            CheckCellPro rs = new CheckCellPro();
            rs.username = tmp[1];
            rs.row = Integer.parseInt(tmp[2]);
            rs.column = Integer.parseInt(tmp[3]);
            rs.value = Integer.parseInt(tmp[4]);
            return rs;
        }
        return null;
    }
    public static String getPlayRequest(String username){
        return PLAY_REQUEST.concat(BLANK).concat(username);
    }
    public static PlayRequestPro explainPlayRequest(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 2){
            PlayRequestPro rs = new PlayRequestPro();
            rs.username = tmp[1];
            return rs;
        }
        return null;
    }
    public static String getLoginSuccess(List<String> userFree, List<String> userBusy){
        String rs = LOGIN_SUCCESS.concat(STAR);
        Iterator<String> iter = userFree.iterator();
        while(iter.hasNext())
            rs = rs.concat(iter.next()).concat(BLANK);
        rs = rs.substring(0, rs.length() - 1);
        rs = rs.concat(STAR);
        iter = userBusy.iterator();
        while(iter.hasNext())
            rs = rs.concat(iter.next()).concat(BLANK);
        return rs.substring(0, rs.length() - 1);
    }
    public static LoginSuccessPro explainLoginSuccess(String message){
        LoginSuccessPro rs = new LoginSuccessPro();
        rs.userFree = new LinkedList<String>();
        rs.userBusy = new LinkedList<String>();
        System.out.println(message);
        String[] tmp = message.split(STAR);
        if(tmp.length > 0 && tmp[0].equals(LOGIN_SUCCESS)){
            if(tmp.length > 1 && !tmp[1].equals("")){
                String[] free = tmp[1].split(BLANK);
                rs.userFree.addAll(Arrays.asList(free));
            }
            if(tmp.length > 2){
                String[] busy = tmp[2].split(BLANK);
                rs.userBusy.addAll(Arrays.asList(busy));
            }
            
            return rs;
        }
        return null;
    }
    public static String getNew(String username){
        return NEW.concat(BLANK).concat(username);
    }
    public static NewPro explainNew(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 2){
            NewPro rs = new NewPro();
            rs.username = tmp[1];
            return rs;
        }
        return null;
    }
    public static String getStart(String username, int pawnValue){
        return START.concat(BLANK).concat(username).concat(BLANK).concat(String.valueOf(pawnValue));
    }
    public static StartPro explainStart(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 3){
            StartPro rs = new StartPro();
            rs.username = tmp[1];
            rs.pawnValue = Integer.parseInt(tmp[2]);
            return rs;
        }
        return null;
    }
    public static String getBusy(String username1, String username2){
        return BUSY.concat(BLANK).concat(username1).concat(BLANK).concat(username2);
    }
    public static BusyPro explainBusy(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 3){
            BusyPro rs = new BusyPro();
            rs.username1 = tmp[1];
            rs.username2 = tmp[2];
            return rs;
        }
        return null;
    }
    public static String getWin(int r, int c, char po, int pownValue){
        return WIN.concat(BLANK).concat(String.valueOf(r)).concat(BLANK)
                .concat(String.valueOf(c)).concat(BLANK).concat(String.valueOf(po))
                .concat(BLANK).concat(String.valueOf(pownValue));
    }
    public static WinPro explainWin(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 5){
            WinPro rs = new WinPro();
            rs.row = Integer.parseInt(tmp[1]);
            rs.column = Integer.parseInt(tmp[2]);
            rs.position = tmp[3].charAt(0);
            rs.pownValue = Integer.parseInt(tmp[4]);
            return rs;
        }
        return null;
    }
    public static String getLost(int r, int c, char po, int pownValue){
        return LOST.concat(BLANK).concat(String.valueOf(r)).concat(BLANK)
                .concat(String.valueOf(c)).concat(BLANK).concat(String.valueOf(po))
                .concat(BLANK).concat(String.valueOf(pownValue));
    }
    public static LostPro explainLost(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 5){
            LostPro rs = new LostPro();
            rs.row = Integer.parseInt(tmp[1]);
            rs.column = Integer.parseInt(tmp[2]);
            rs.position = tmp[3].charAt(0);
            rs.pownValue = Integer.parseInt(tmp[4]);
            return rs;
        }
        return null;
    }
    public static String getFree(String username1, String username2){
        return FREE.concat(BLANK).concat(username1).concat(BLANK).concat(username2);
    }
    public static FreePro explainFree(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 3){
            FreePro rs = new FreePro();
            rs.username1 = tmp[1];
            rs.username2 = tmp[2];
            return rs;
        }
        return null;
    }
    public static String getLeave(String username1, String username2){
        return LEAVE.concat(BLANK).concat(username1).concat(BLANK).concat(username2);
    }
    public static LeavePro explainLeave(String message){
        String tmp[] = message.split(BLANK);
        if(tmp.length == 3){
            LeavePro rs = new LeavePro();
            rs.username1 = tmp[1];
            rs.username2 = tmp[2];
            return rs;
        }
        return null;
    }
    public static String getAuth(String username, String password){
        return AUTH.concat(BLANK).concat(username).concat(BLANK).concat(password);
    }
    public static AuthClientPro explainAuth(String message){
        String[] tmp = message.split(BLANK);
        if(tmp.length == 3){
            AuthClientPro rs = new AuthClientPro();
            rs.username = tmp[1];
            rs.password = tmp[2];
            return rs;
        }
        return null;
    }
    public static String getAccept(String username){
        return ACCEPT.concat(BLANK).concat(username);
    }
    public static AcceptClientPro explainAccept(String message){
        String[] tmp = message.split(BLANK);
        if(tmp.length == 2){
            AcceptClientPro rs = new AcceptClientPro();
            rs.username = tmp[1];
            return rs;
        }
        return null;
    }
    public static String getRefuse(String username){
        return REFUSE.concat(BLANK).concat(username);
    }
    public static RefuseClientPro explainRefuse(String message){
        String[] tmp = message.split(BLANK);
        if(tmp.length == 2){
            RefuseClientPro rs = new RefuseClientPro();
            rs.username = tmp[1];
            return rs;
        }
        return null;
    }
    public static String getExit(String username){
        return EXIT.concat(BLANK).concat(username);
    }
    public static ExitClientPro explainExit(String message){
        String[] tmp = message.split(BLANK);
        if(tmp.length == 2){
            ExitClientPro rs = new ExitClientPro();
            rs.username = tmp[1];
            return rs;
        }
        return null;
    }
    public static String getProtocol(String message){
        String tmp[] = message.split(BLANK);
        if(tmp != null && tmp.length > 0)
            return tmp[0];
        return null;
    }
}